package com.gx.smart.construction.ui.house.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.gx.smart.base.BaseMVVMFragmentV2;
import com.gx.smart.base.BaseMVVMFragmentV3;
import com.gx.smart.construction.R;
import com.gx.smart.construction.databinding.SmartHomeRecordFragmentV2Binding;
import com.gx.smart.construction.databinding.SmartHomeRecordFragmentV3Binding;
import com.gx.smart.construction.ui.common.fragment.BaseMVVMRefreshFragmentV3;
import com.gx.smart.construction.ui.house.adapter.RecordItemAdapterWithDiff;
import com.gx.smart.construction.ui.house.adapter.RecordItemPagingAdapter;
import com.gx.smart.construction.ui.house.model.RecordType;
import com.gx.smart.construction.ui.house.model.RoomInfo;
import com.gx.smart.construction.ui.house.viewmodel.SmartHomeRecordViewModel;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfo;
import com.mylhyl.circledialog.CircleDialog;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class SmartHomeRecordFragmentV3 extends BaseMVVMFragmentV2<SmartHomeRecordViewModel> implements OnRefreshListener{
    @Inject
    RecordItemPagingAdapter adapter;
    private SmartHomeRecordFragmentV3Binding dataBinding;
    private final List<RecordType> items = new ArrayList<>();
    private int type = 3;

    @Override
    public int onBindLayout() {
        return R.layout.smart_home_record_fragment_v3;
    }

    @Override
    public String getToolbarTitle() {
        return "添加记录";
    }

    @Override
    protected Class<SmartHomeRecordViewModel> onBindViewModel() {
        return SmartHomeRecordViewModel.class;
    }

    @Override
    public void initContentView(ViewGroup root) {
        dataBinding = DataBindingUtil.inflate(getLayoutInflater(), onBindLayout(), root, true);
        dataBinding.setLifecycleOwner(getViewLifecycleOwner());
        dataBinding.setSmartHomeRecordFragment(this);
        dataBinding.setSmartHomeRecordViewModel(viewModel);
        dataBinding.refreshLayout.setOnRefreshListener(this);
        dataBinding.recyclerView.setAdapter(adapter);
        dataBinding.refreshLayout.setEnableLoadMore(false);
        DividerItemDecoration divider = new DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL);
        @SuppressLint("UseCompatLoadingForDrawables")
        Drawable drawable = getResources().getDrawable(R.drawable.shape_list_item_divider_line);
        divider.setDrawable(drawable);
        dataBinding.recyclerView.addItemDecoration(divider);
    }

    @Override
    protected void initData() {
        RecordType item1 = new RecordType();
        item1.setType(3);
        item1.setContent("全部");
        items.add(item1);

        RecordType item2 = new RecordType();
        item2.setType(1);
        item2.setContent("未绑定");
        items.add(item2);


        RecordType item3 = new RecordType();
        item3.setType(2);
        item3.setContent("已绑定");
        items.add(item3);
        dataBinding.refreshLayout.autoRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        viewModel.refresh();
    }


    @SuppressWarnings("unused")
    public void selectRoomType(View v) {
        new CircleDialog.Builder()
                .setItems(items, (parent, view, position, id) -> {
                    RecordType item = items.get(position);
                    type = item.getType();
                    viewModel.setSelectedType(item.getContent());
                    dataBinding.refreshLayout.autoRefresh();
                    return true;
                })
                .setNegative("取消", null)
                .show(requireActivity().getSupportFragmentManager());
    }

    @Override
    protected void initObserver() {
        super.initObserver();
        viewModel.roomInfoPagedList.observe(this, list -> {
            dataBinding.refreshLayout.finishRefresh();
            if (list.isEmpty()) {
                showNoDataView(true);
                return;
            }
            showNoDataView(false);
            adapter.submitList(list);
        });
    }
}